package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.HandleAlertDataDto;
import com.zzyl.nursing.util.DateTimeZoneConverter;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        alertData.setUserId(userId);
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Autowired
    private IAlertDataService alertDataService;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 处理报警数据
     *
     * @param dto
     */
    @Override
    public void handleAlertData(HandleAlertDataDto dto) {
//        AlertData alertData = alertDataMapper.selectAlertDataById(dto.getId());
//        //获取处理人
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        //获取处理时间
//        String processingTime = dto.getProcessingTime();
//        LocalDateTime localDateTime = LocalDateTimeUtil.parse(processingTime, DatePattern.UTC_WITH_ZONE_OFFSET_PATTERN);
//        LocalDateTime time = DateTimeZoneConverter.utcToShanghai(localDateTime);
//        //获取处理结果
//        String processingResult = dto.getProcessingResult();
//        //设置处理结果
//        alertData.setProcessingResult(processingResult);
//        //设置处理人
//        alertData.setProcessorName(JSONUtil.toJsonStr(loginUser));
//        //设置处理时间
//        alertData.setProcessingTime(time);
//        //设置状态
//        alertData.setStatus(1);

        AlertData alertData = BeanUtil.toBean(dto, AlertData.class);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        String nickName = sysUser.getNickName();
        alertData.setProcessorName(nickName);
        alertData.setStatus(1);
        //获取处理时间
        String processingTime = dto.getProcessingTime();
        LocalDateTime localDateTime = LocalDateTimeUtil.parse(processingTime, DatePattern.UTC_MS_PATTERN);
        LocalDateTime time = DateTimeZoneConverter.utcToShanghai(localDateTime);
        alertData.setProcessingTime(time);
        String iotId = alertDataMapper.selectIotIdById(alertData.getId());
        Date createTime = alertDataMapper.selectCreateTimeById(alertData.getId());
        alertData.setIotId(iotId);
        alertData.setCreateTime(createTime);
        alertDataMapper.updateDataByIotIdAndCreateTime(alertData);

    }
}
